package com.pxxy.lib_common.base;

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.gyf.immersionbar.ImmersionBar;
import com.kaopiz.kprogresshud.KProgressHUD;
import com.pxxy.lib_common.R;
import com.pxxy.lib_common.inter.IView;
import com.pxxy.lib_common.utils.ActivityCollector;
import com.pxxy.lib_common.utils.DialogUtils;
import com.pxxy.lib_common.utils.L;
import com.pxxy.lib_common.utils.SystemUtils;
import com.pxxy.lib_common.utils.TipDialogView;

public abstract class BaseActivity extends AppCompatActivity implements IView {
    //是否显示状态栏
    private boolean isShowState = true;
    //封装Toast对象
    private static Toast toast = null;
    //Log_Tag
    private static final String BASELOGTAG = "currentActivity";
    //提示dialog
    private TipDialogView tipDialog;
    //对话框dialog
    private DialogUtils alertDialog;
    //进度条
    private KProgressHUD progressBar;
    //判断是否是沉浸式状态栏
    public boolean isImmersionBar = true;
    /**
     * 无网络连接
     */
    public static final int TOAST_TYPE_NO_INTERNET = 101;
    /**
     * 服务器繁忙
     */
    public static final int TOAST_TYPE_SERVER_ERROR = 102;

    public static final int TIPDIALOG_TYPE_NO_INTERNET = 101;
    public static final int TIPDIALOG_TYPE_SERVER_ERROR = 102;
    //提示框类型
    /**
     * 显示失败图标
     */
    public static final int HUD_TYPE_FAIL = 0 ;
    /**
     * 显示成功图标
     */
    public static final int HUD_TYPE_SUCCESS = 1 ;
    /**
     * 不显示任何图标
     */
    public static final int HUD_TYPE_NOTHING = 2 ;
    /**
     * 显示信息图标
     */
    public static final int HUD_TYPE_INFO = 3 ;

    public static final String TOAST_NO_INTERNET = "网络连接失败!";

    public static final String TOAST_SERVER_ERROR = "服务器繁忙!";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //只允许竖屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        //判断是否显示状态栏
        if (!isShowState) {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }

        //沉浸式状态栏
        if (isImmersionBar){
            ImmersionBar.with(this).keyboardEnable(true).statusBarColor(R.color.themeColor).init();
        }
        //设置视图
        setContentView(initLayout());
        //初始化控件
        initView();
        //设置数据
        initData();
        //初始化事件
        initEvent();
        //log打印当前activity位置
        L.d(BASELOGTAG,getClass().getSimpleName());
        //添加当前activity到ActivityCOllector中
        ActivityCollector.addActivity(this);
    }

    /**
     * 设置页面视图
     * @return
     */
    public abstract  int initLayout();

    /**
     * 初始化数据
     */
    public void initData(){

    }

    /**
     * 初始化控件
     */
    public void initView(){

    }

    /**
     * 初始化事件
     */
    public void initEvent(){

    }


    /**
     * 设置是否显示状态栏
     *
     * @param isShow
     */
    public void setState(boolean isShow) {
        isShowState = isShow;
    }

    /**
     * 显示短Toast
     * @param msg 要显示的信息
     */
    public void toastShort(String msg) {
        //由于8.0以上系统toast优化过，所以进行判断，进行不同的toast操作
        if (SystemUtils.getSystemVersionOnRoughly() < 8){
            if (toast == null) {
                toast = Toast.makeText(this,msg,Toast.LENGTH_SHORT);
            } else {
                toast.setText(msg);
            }
            toast.show();
        }else {
            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        }

    }

    /**
     * 显示长Toast
     *
     * @param msg 要显示的信息
     */
    public void toastLong(String msg) {
        //由于8.0以上系统toast优化过，所以进行判断，进行不同的toast操作
        if (SystemUtils.getSystemVersionOnRoughly() < 8){
            if (toast == null) {
                toast = Toast.makeText(this,msg,Toast.LENGTH_LONG);
            } else {
                toast.setText(msg);
            }
            toast.show();
        }else {
            Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
        }
    }

    /**
     *显示提示框
     * @param msg 显示的消息
     * @param type  决定是否显示或者显示什么icon，activity中存在常量HUD_TYPE开头
     * @param isDismiss 是否自动消失，默认1秒消失
     */
    @Override
    public void showTipDialog(String msg,int type,boolean isDismiss) {
        if (type == HUD_TYPE_FAIL){
            tipDialog = new TipDialogView
                    .Builder(this)
                    .setIconType(TipDialogView.Builder.ICON_TYPE_FAIL)
                    .setTipWord(msg)
                    .create();
        }else if (type == HUD_TYPE_SUCCESS){
            tipDialog = new TipDialogView
                    .Builder(this)
                    .setIconType(TipDialogView.Builder.ICON_TYPE_SUCCESS)
                    .setTipWord(msg)
                    .create();
        }else if (type == HUD_TYPE_NOTHING){
            tipDialog = new TipDialogView
                    .Builder(this)
                    .setTipWord(msg)
                    .create();
        }else if (type == HUD_TYPE_INFO){
            tipDialog = new TipDialogView
                    .Builder(this)
                    .setIconType(TipDialogView.Builder.ICON_TYPE_INFO)
                    .setTipWord(msg)
                    .create();
        }
        //显示tipDialog
        if (tipDialog != null){
            tipDialog.show();
        }

        //判断是否自动消失，默认1秒自动消失
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                dismissTipDialog();
            }
        },1000);
    }

    @Override
    public void showTipDialog(int code) {
        if (code == TIPDIALOG_TYPE_NO_INTERNET){
            showTipDialog(TOAST_NO_INTERNET,HUD_TYPE_INFO,true);
        }else if (code == TIPDIALOG_TYPE_SERVER_ERROR){
            showTipDialog(TOAST_SERVER_ERROR,HUD_TYPE_INFO,false);
        }
    }

    /**
     * 隐藏提示框
     */
    @Override
    public void dismissTipDialog() {
        //关闭tipDialog
        if (tipDialog != null){
            tipDialog.dismiss();
        }
    }

    /**
     *只有一个确定按钮的alertDialog,无法添加点击事件
     * @param title 标题
     * @param msg   内容
     * @param Cancelable dialog 点击对话框以外的区域是否让对话框消失,默认为true；
     */
    @Override
    public void showAlertDialog(String title, String msg,boolean Cancelable) {
        alertDialog = new DialogUtils()
                .initDialog(this,title,msg,Cancelable)
                .showDialog();
    }

    @Override
    public void showAlertDialog(String title, String msg, boolean Cancelable, String leftBtnName, String rightBtnName, View.OnClickListener leftBtnListener, View.OnClickListener rightBtnListener) {
        alertDialog = new DialogUtils()
                .initDialog(this,title,msg,Cancelable)
                .setLeftButton(leftBtnName,leftBtnListener)
                .setRightButton(rightBtnName,rightBtnListener)
                .showDialog();
    }

    /**
     * 隐藏对话框
     */
    @Override
    public void dismissAlertDialog() {
        if (alertDialog != null){
            alertDialog.closeDialog();
        }
    }

    /**
     * 显示加载框，只有菊花样式
     * @param title 显示的内容
     * @param dimAmount 加载框外的透明度，取值0-1，默认为0
     */
    @Override
    public void showProgressBar(String title,float dimAmount) {
        float dim = 0;
        if (dimAmount <=1 && dimAmount >= 0){
            dim = dimAmount;
        }
        progressBar = KProgressHUD.create(this)
                .setStyle(KProgressHUD.Style.SPIN_INDETERMINATE)
                .setSize(120,120)
                .setDimAmount(dim)
                .setLabel(title);
        //显示progressBar
        progressBar.show();
    }

    @Override
    public void dismissProgressBar() {
        if (progressBar != null){
            progressBar.dismiss();
        }
    }

    @Override
    public void showLoading() {
    }

    @Override
    public void showEmptyView() {

    }

    @Override
    public void showError() {

    }

    @Override
    public void toastError(int code) {
        if(code == TOAST_TYPE_NO_INTERNET){
            toastShort(TOAST_NO_INTERNET);
        }else if (code == TOAST_TYPE_SERVER_ERROR){
            toastShort(TOAST_SERVER_ERROR);
        }
    }



    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //从ActivityCollector中移除该activity
        ActivityCollector.removeActivity(this);
    }

    /**
     * 关闭activity
     */
    public void closeActivity(){
        this.finish();
    }
}
